import datetime
import random
import time


class Serce_integration:
    def __init__(self, zhi):
        self.__dict__.update(zhi)

    def response_seach(self):
        response = self.session.post(self.url_switch+'/wlwaq-server//logQuery/initByPage.action', headers=self.headers,
                                           data=self.url_switch_one_data, verify=False)
        self.my_logg.info("安全平台正在查询任务...")
        if response.status_code == 200:
            response = response.json()['data']
            if response:
                time.sleep(random.randint(10, 20))
                for response_can in response:
                    self.my_logg.info(response_can)
                    if response_can:
                        self.my_logg.info("安全平台正在签到")
                        ids = response_can['id']
                        index = response_can['_index_']
                        time_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                        comment_list = [{"name": "NOC", "date": time_date, "action": "\u5DF2\u7B7E\u6536", "comment": ""}]
                        alertIds = [{"ids": ids, "index": index, "comment_list": comment_list}]
                        # self.events.append(alertIds)
                        self.events = f'{alertIds}'
                        self.my_logg.info(self.events)
                        self.singfor()
            else:
                self.my_logg.info("安全平台没有需要签到的任务")
        else:
            self.my_logg.info("cookie失效")

    # 签到#
    def singfor(self):
        data = {
            'name': 'NOC',
            'gender': 'false',
            'status': '0',
            'alertIds': self.events,
            'deal_mid_note': ''
        }
        response = self.session.post(self.url_switch+'/wlwaq-server//logQuery/updateEsReview.action', headers=self.headers,
                                           data=data, verify=False)
        self.my_logg.info(response.status_code)
        self.my_logg.info(response)
        response = response.json()
        if response['result_msg'] == "成功":
            self.my_logg.info("安全平台签收成功")
            return "安全平台签收成功"
        else:
            self.my_logg.info("安全平台签收失败")
            return "安全平台签收失败"